
import '../../sass/auth/PermissionEdit.scss';
import router from '../../router/Index.js';

export default {
    data(){
        let checkTitle = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('权限名称不能为空'));
            }
            if (value.length < 4) {
                callback(new Error('请至少输入4个字符'));
            } else {
                callback();
            }
        };
        let checkController = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('控制器不能为空'));
            }
            let str = /^[A-Za-z]*$/;
            if (!str.test(value)) {
                callback(new Error('请输入字母'));
            } else {
                callback();
            }
        };
        let checkOperation = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('控制器不能为空'));
            }
            let str = /^[A-Za-z]*$/;
            if (!str.test(value)) {
                callback(new Error('请输入字母'));
            } else {
                callback();
            }
        };
        return {
            id:0,
            type:'添加',
            labelPosition: 'left',
            loading:false,
            userData:'',
            PermissionForm: {
                title: '',
                controller: '',
                operation: ''
            },
            rules: {
                title: [
                    { validator: checkTitle, trigger: 'blur' }
                ],
                controller: [
                    { validator: checkController, trigger: 'blur' }
                ],
                operation: [
                    { validator: checkOperation, trigger: 'blur' }
                ],
            }
        }
    },
    components:{

    },
    created:function () {
        if(this.$route.query.id) {
            this.id = this.$route.query.id;
            this.type = '编辑';
            this.fetchData();
        }
    },
    computed: {

    },
    filters:{

    },
    methods: {
        submitForm(formName) {
            let _this = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.loading = true;
                    if(_this.id > 0 ){
                        var data = {
                            title:_this.PermissionForm.title,
                            controller:_this.PermissionForm.controller,
                            operation:_this.PermissionForm.operation,
                            id: _this.id
                        };
                        var request_url = "/backend/auth/editPermission";
                    }else{
                        var data = {
                            title:_this.PermissionForm.title,
                            controller:_this.PermissionForm.controller,
                            operation:_this.PermissionForm.operation,
                        };
                        var request_url = "/backend/auth/addPermission";
                    }
                    $.ajax({
                        type:"POST",
                        url: request_url,
                        data:data,
                        dataType:"json",
                        success:function(result){
                            if(result.code===0){
                                _this.$message({
                                    message: result.msg,
                                    type: 'success'
                                });
                                _this.loading = false;
                                _this.backToPermission();
                            }else if(result.code ===-1){
                                _this.$message({
                                    message: result.msg,
                                    type: 'warning'
                                });
                            }else if(result.code ===-2){
                                _this.$message({
                                    message: result.msg,
                                    type: 'error'
                                });
                            }else if(result.code ===-999){
                                _this.$message({
                                    message: result.msg,
                                    type: 'error'
                                });
                                setTimeout(function () {
                                    window.location.reload();
                                },1500);
                            }
                            _this.loading = false;
                        },
                        error:function () {
                            _this.$message({
                                message: '调用接口失败',
                                type: 'error'
                            });
                            _this.loading = false;
                        }
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            if(this.id <=0)
                this.$refs[formName].resetFields();
            else
                this.setParams(this.userData);
        },
        backToPermission(){
            router.push({path:'/auth/permission'});
        },
        async fetchData() {
            let _this=this,response,_response;
            let url = '/backend/auth/permission';
            this.loading = true;
            try {
                response = await fetch(url, {
                    method: 'POST',
                    headers: {
                        'Accept': 'application/json',
                        'Content-type': "application/json",
                    },
                    credentials: 'same-origin',
                    body:JSON.stringify({
                        'id' : _this.id,
                    })
                });
                _response = await response.json();

                if(_response.code===0){
                    _this.userData = _response.data;
                    _this.setParams(_this.userData);
                }else if(_response.code ===-2){
                    _this.$message({
                        message: _response.msg,
                        type: 'error'
                    });
                }else if(_response.code ===-999){
                    _this.$message({
                        message: _response.msg,
                        type: 'error'
                    });
                    setTimeout(function () {
                        window.location.reload();
                    },1500);
                }
                _this.loading = false;
            } catch(err) {
                _this.$message({
                    message: '调用接口失败',
                    type: 'error'
                });
                _this.loading = false;
            }
        },
        setParams(data) {
            let _this = this;
            _this.PermissionForm.title = data.title;
            _this.PermissionForm.controller = data.controller;
            _this.PermissionForm.operation = data.operation;
        }
    }
}